package com.gitee.huanminabc.dynamicdb.filters;

import com.gitee.huanminabc.dynamicdb.common.Constants;
import com.gitee.huanminabc.dynamicdb.common.SpringContextHolder;
import com.gitee.huanminabc.dynamicdb.config.DynamicDbSpringConfig;
import com.gitee.huanminabc.dynamicdb.core.DBContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
@Slf4j
@Activate(order = Integer.MIN_VALUE, group = {CommonConstants.CONSUMER})
//客户端请求服务端之前的拦截器
public class ConsumerDubboDynamicDbFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
//        log.info("ConsumerDubboDynamicDbFilter");
        DynamicDbSpringConfig dynamicDbSpringConfig =(DynamicDbSpringConfig) SpringContextHolder.getBean(DynamicDbSpringConfig.class);
        RpcContext context = RpcContext.getContext();
        if (dynamicDbSpringConfig.isMultiTenantMode()) {
            String tenantKeys = DBContext.tenantKeys.get();
            if (tenantKeys != null) {
                context.setAttachment(Constants.TENANT_KEY,tenantKeys);
            }
        }
        return invoker.invoke(invocation);
    }
}